home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / C and C++ / Text⁄Files / macgzip_02-src / macos / think / MacGzip.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-12-22  |  8.8 KB  |  461 lines  |  [TEXT/KAHL]

  1. /*
  2.  * Copyright (C) 1993  SPDsoft
  3.  * 
  4.  */
  5.  
  6. #include <GestaltEqu.h>
  7. #include <Sound.h>
  8. /*#include <EPPC.h>*/
  9.  
  10. #include "MacGzip.h"
  11. #include "thePrefs.h"
  12. #include "MacErrors.h"
  13. #include "SPDProg.h"
  14.  
  15. #include <string.h>
  16.  
  17. void    about(void);
  18. void    InitTheMac(void);
  19. int        OpenFromFinder( void );
  20. void    MyBeep(void);
  21.  
  22. void CenterPicRect(PicHandle thePic, Rect *inRect, Rect *outRect);
  23.  
  24.  
  25. static    pascal char
  26.         MyFilter ( DialogPtr theDialog, EventRecord *theEvent,short  *itemHit );
  27.  
  28. extern OSErr
  29.         DoOpen( Str255 fName, long vRefNum);
  30.  
  31. /* CONST */
  32. #define NIL 0L
  33.  
  34. static SFReply    reply;
  35. SFTypeList        typeList =    {'Gzip','ZIVU','pZIP','ZIVM'};
  36. static Point    myPoint =    {80,80};
  37.  
  38. Boolean            quitting = false;
  39.  
  40. MenuHandle        myMenus[4];
  41. OSErr            theOSErr;
  42. KeyMap            theKeys;
  43.  
  44.  
  45. void InitTheMac(void)
  46. {
  47. long gestAliasResponse, gestStdFileResponse, gestFSResponse;
  48.  
  49.     FlushEvents(everyEvent, 0);
  50.     InitGraf(&qd.thePort);
  51.     InitFonts();
  52.     InitWindows();
  53.     InitMenus();
  54.     TEInit();
  55.     InitDialogs(NIL);
  56.     InitCursor();
  57.     MaxApplZone();
  58.  
  59.     GetKeys(&theKeys);
  60.  
  61. /* sys 7 ?*/
  62.  
  63.     theOSErr = Gestalt(gestaltAliasMgrAttr, &gestAliasResponse);
  64.     if (theOSErr == noErr) theOSErr = Gestalt(gestaltFSAttr, &gestFSResponse);
  65.     if (theOSErr == noErr)
  66.         theOSErr = Gestalt(gestaltStandardFileAttr, &gestStdFileResponse);
  67.     
  68.     if (theOSErr != noErr ||
  69.         (gestAliasResponse & (1 << gestaltAliasMgrPresent)) == 0 ||
  70.         (gestFSResponse & (1 << gestaltHasFSSpecCalls)) == 0 ||
  71.         (gestStdFileResponse & (1 << gestaltStandardFile58)) == 0) {
  72.     
  73.         theAlert( OLD_SYSTEM,GENERIC, theOSErr, true );
  74.     }
  75.     
  76.     CanBreak=true;
  77.  
  78.     if (!GetOrGeneratePrefs(&currPrefs, kCurrentPrefsTypeVers))
  79.     {
  80.         about();
  81.         prefsChangedFlag = DoPrefsDialog(&currPrefs);
  82.         if (prefsChangedFlag)
  83.         {
  84.             if( ( theOSErr = SavePrefs(&currPrefs)) != noErr )
  85.                     theAlert(NO_SAVE_PREF,GENERIC,theOSErr, true );
  86.         }
  87.         else ExitToShell();
  88.     }
  89.  
  90. }
  91.  
  92. main()
  93.  
  94. { /* main program */
  95.     extern    Boolean        gSavedPos;
  96.     extern    Point        gSavedPoint;
  97.  
  98.     InitTheMac();
  99.     SetUpMenus();
  100.     
  101.     if ( 0!=OpenFromFinder() )
  102.     {
  103.         quitting=true;
  104.     }
  105.     
  106.     while( !quitting )
  107.     {
  108.         MainEvent();
  109.     }
  110.  
  111. /* remember window pos */
  112.             
  113.     currPrefs.SavedPos = gSavedPos; /* always true */
  114.         
  115.     if(( gSavedPoint.h != currPrefs.SavedPoint.h ) ||
  116.         ( gSavedPoint.v != currPrefs.SavedPoint.v ) )
  117.     {
  118.         currPrefs.SavedPoint.h = gSavedPoint.h;
  119.         currPrefs.SavedPoint.v = gSavedPoint.v;
  120.         prefsChangedFlag=true;
  121.     }
  122.  
  123.     if (prefsChangedFlag)
  124.     {
  125.         if( ( theOSErr = SavePrefs(&currPrefs)) != noErr )
  126.                 theAlert(NO_SAVE_PREF,GENERIC,theOSErr, true );
  127.     }
  128.     
  129.     
  130.     ExitToShell();
  131. }
  132.  
  133.  
  134. void MainEvent(void)
  135. {
  136.     EventRecord myEvent;
  137.     WindowPtr whichWindow;
  138.     short windowPart;
  139.  
  140.     SystemTask();
  141.     if( GetNextEvent(everyEvent, &myEvent) ) {
  142.         switch( myEvent.what ) {
  143.             case mouseDown:
  144.                 windowPart = FindWindow(myEvent.where, &whichWindow);
  145.                 DoMouseDown(windowPart, whichWindow, &myEvent);
  146.                 break;
  147.                 
  148.             case keyDown:
  149.             case autoKey: 
  150.             {
  151.                 register char theChar;
  152.     
  153.                 theChar = myEvent.message & charCodeMask;
  154.                 if ((myEvent.modifiers & cmdKey) != 0) 
  155.                     DoCommand( MenuKey(theChar) );
  156.                                     
  157.                 break;
  158.             }
  159.                 
  160.             case activateEvt:
  161.                 break;
  162.  
  163.             case updateEvt: 
  164.                 break;
  165.                 
  166.             case diskEvt:
  167.             if ((myEvent.message >> 16) != noErr)
  168.             {
  169.                 theOSErr = DIBadMount(myPoint, myEvent.message);
  170.                 theAlert(BAD_DISK,GENERIC,theOSErr,false);
  171.             }
  172.             break;
  173.  
  174.             case osEvt:
  175.             switch ((myEvent.message >> 24) & 0x0ff)
  176.             {
  177.                 case suspendResumeMessage:
  178.                     
  179.                     SetCursor(&qd.arrow);
  180.                 
  181.                     if ((myEvent.message & resumeFlag) == 0) // suspend 
  182.                     {
  183.                     }
  184.                     else
  185.                     {                                        // resume
  186.                     }
  187.                     break;
  188.                     
  189.                 case mouseMovedMessage:
  190.                     break;
  191.             }
  192.             break;
  193.                 
  194. /*            case kHighLevelEvent:*/
  195. /*                break;*/
  196.  
  197.  
  198.         }
  199.     }
  200. }
  201. void DoCommand(long mResult)
  202. {
  203.     short    theItem;
  204.     Str255    name;
  205.  
  206.     theItem = LoWord(mResult);
  207.     switch( HiWord(mResult) ) {
  208.         case appleID:
  209.             if( theItem > 2 ) {
  210.                 CGrafPtr savePort;
  211.                 GetItem(myMenus[appleM], theItem, &name);
  212.                 GetPort(&savePort);
  213.                 OpenDeskAcc(name);
  214.                 SetPort(savePort);
  215.             }
  216.             else
  217.             {
  218.                 about();
  219.             }
  220.             break;
  221.  
  222.         case fileID:
  223.             DoFile(theItem);
  224.             break;
  225.  
  226.         case editID:
  227.             SystemEdit(theItem-1);
  228.             break;
  229.             
  230.         case gzipID:
  231.         
  232.             switch(theItem){
  233.             
  234.             case gmAscii:
  235.             
  236.                 currPrefs.ascii=true;
  237.                 CheckItem( myMenus[gzipM], gmBin, false );
  238.                 CheckItem( myMenus[gzipM], gmAscii, true);
  239.                 break;
  240.  
  241.             case gmBin:
  242.             
  243.                 currPrefs.ascii=false;
  244.                 CheckItem( myMenus[gzipM], gmAscii , false );
  245.                 CheckItem( myMenus[gzipM], gmBin, true);
  246.                 break;
  247.             
  248.             case gmComp:
  249.             
  250.                 currPrefs.compress=true;
  251.                 CheckItem( myMenus[gzipM], gmUncomp, false );
  252.                 CheckItem( myMenus[gzipM], gmComp, true);
  253.                 break;
  254.  
  255.             case gmUncomp:
  256.             
  257.                 currPrefs.compress=false;
  258.                 CheckItem( myMenus[gzipM], gmComp , false );
  259.                 CheckItem( myMenus[gzipM], gmUncomp, true);
  260.                 break;
  261.             
  262.             }
  263.             prefsChangedFlag = true;
  264.             break;
  265.     }
  266.  
  267.     HiliteMenu(0);
  268. }
  269.  
  270. void DoFile(short item)
  271. {
  272.     switch( item ) {
  273.         case fmOpen:
  274.         
  275.             SFGetFile(
  276.                         myPoint,
  277.                         "\p",
  278.                         0L,
  279.                         -1,
  280. /*                        currPrefs.compress ? -1:4,*/
  281.                         typeList,
  282.                         0L,
  283.                         &reply
  284.                     );
  285.             
  286.             if( reply.good != false )
  287.             {
  288.                 if( DoOpen( reply.fName, reply.vRefNum )!=128)
  289.                 {
  290.                     if(currPrefs.BeepWhenDone) MyBeep();
  291.                     if(currPrefs.QuitWhenDone) quitting=true;
  292.                 }
  293.             }
  294.             break;
  295.  
  296.         case fmPrefs:
  297.         
  298.             prefsChangedFlag = DoPrefsDialog(&currPrefs);
  299.             if (prefsChangedFlag)
  300.             {
  301.                 if( ( theOSErr = SavePrefs(&currPrefs)) != noErr )
  302.                         theAlert(NO_SAVE_PREF,GENERIC,theOSErr, true );
  303.             }
  304.             break;
  305.  
  306.         case fmQuit:
  307.         
  308.             quitting = true;
  309.             break;
  310.     }
  311. }
  312. void DoMouseDown(short windowPart, WindowPtr whichWindow, EventRecord *myEvent)
  313. {
  314.     switch( windowPart ) {
  315.         case inGoAway:
  316.             break;
  317.  
  318.         case inMenuBar:
  319.             DoCommand( MenuSelect(myEvent->where) );
  320.             break;
  321.  
  322.         case inSysWindow:
  323.             SystemClick(myEvent, whichWindow);
  324.             break;
  325.  
  326.         case inDrag:
  327.             break;
  328.  
  329.         case inGrow:
  330.             break;
  331.  
  332.         case inContent:
  333.             if( whichWindow != FrontWindow() )
  334.                 SelectWindow(whichWindow);
  335.             break;
  336.     }
  337. }
  338.  
  339. int OpenFromFinder( void )
  340. {
  341.     int    message, count, index;
  342.     AppFile        theFile;
  343.     Boolean PrefCSave,PrefASave, HasToBeep=false;
  344.     
  345.     PrefCSave =    currPrefs.compress;
  346.     currPrefs.compress = (( IsOptKey(theKeys) ) ? true : false);
  347.     PrefASave =    currPrefs.ascii;
  348.     if (IsAKey(theKeys)) currPrefs.ascii = true;
  349.     else if (IsBKey(theKeys)) currPrefs.ascii = false;
  350.     
  351.     CountAppFiles( &message, &count);            /*any files to open?*/
  352.     for( index= 1 ; index <= count ; index ++)
  353.     {
  354.         GetAppFiles(index, &theFile);
  355.         
  356.         if( DoOpen(theFile.fName, theFile.vRefNum)!=128)
  357.         {
  358.             HasToBeep=currPrefs.BeepWhenDone;
  359.         }
  360.     }
  361.     
  362.     if(HasToBeep) MyBeep();
  363.  
  364.     ClrAppFiles(count);
  365.  
  366.     currPrefs.compress=PrefCSave;
  367.     currPrefs.ascii = PrefASave;
  368.  
  369.     return count;
  370. }
  371.  
  372. /***************************************************************************/
  373. void CenterPicRect(PicHandle thePic, Rect *inRect, Rect *outRect)
  374. {
  375.     short        dh,dv;
  376.     
  377.     *inRect = (*thePic)->picFrame;
  378.     dh = (outRect->right - (inRect->right - inRect->left)) / 2;
  379.     dv = (outRect->bottom - (inRect->bottom - inRect->top)) / 2;
  380.     OffsetRect(inRect,dh,dv);
  381. }
  382.  
  383. void   about( void )
  384. {
  385.     DialogPtr    GetSelection;              
  386.     short        itemHit;                       
  387.     short        DType; 
  388.     Handle        DItem;
  389.     GrafPtr        savePort;
  390.  
  391.     Rect        picRect;
  392.     PicHandle    thePict;
  393.  
  394.     GetSelection = GetNewDialog(dlogAbout, NIL, (WindowPtr)-1);
  395.     ShowWindow(GetSelection);           
  396.     SelectWindow(GetSelection);         
  397.     GetPort(&savePort);
  398.     SetPort(GetSelection);
  399.  
  400.  
  401.     GetDItem(GetSelection, 1, &DType, &DItem, &picRect);
  402.     thePict = (PicHandle) Get1Resource('PICT',ABOUT);
  403.     CenterPicRect(thePict, &picRect, &GetSelection->portRect);
  404.     DrawPicture(thePict,&picRect);
  405.  
  406.     ModalDialog(MyFilter, &itemHit);
  407.     EraseRect (&GetSelection->portRect);
  408.     ReleaseResource(thePict);
  409.     
  410.     thePict = (PicHandle) Get1Resource('PICT',GPL);
  411.     CenterPicRect(thePict, &picRect, &GetSelection->portRect);
  412.     DrawPicture(thePict,&picRect);
  413.     
  414.     ModalDialog(MyFilter, &itemHit);
  415.     ReleaseResource(thePict);
  416.  
  417.     DisposDialog(GetSelection);  
  418.     SetPort(savePort);
  419.     
  420. }                                       
  421. static pascal char
  422.             MyFilter (
  423.                         DialogPtr        theDialog,      
  424.                         EventRecord       *theEvent,    
  425.                         short            *itemHit
  426.             )
  427. {
  428.     return ((theEvent->what == keyDown)||(theEvent->what == mouseDown));
  429. }
  430. /***************************************************************************/
  431. void SetUpMenus(void)
  432. {
  433.     short index;
  434.     
  435.     ClearMenuBar();
  436.     
  437.     myMenus[appleM] = GetMenu(appleID);
  438.     AddResMenu(myMenus[appleM], 'DRVR');
  439.     myMenus[fileM] = GetMenu(fileID);
  440.     myMenus[editM] = GetMenu(editID);
  441.     myMenus[gzipM] = GetMenu(gzipID);
  442.     for(index=appleM; index <= gzipM; index++)
  443.         InsertMenu(myMenus[index], 0) ;
  444.  
  445.     CheckItem( myMenus[gzipM], (currPrefs.ascii?gmAscii:gmBin), true);
  446.     CheckItem( myMenus[gzipM], (currPrefs.compress?gmComp:gmUncomp), true);
  447.     
  448.     DrawMenuBar();
  449. }
  450. /***************************************************************************/
  451. void MyBeep(void)
  452. {
  453.     Handle theSound;
  454.     
  455.         theSound = GetResource('snd ', SOUND_ID);
  456.             if (theSound != nil)
  457.             {
  458.                 SndPlay(nil, theSound, false);
  459.                 ReleaseResource(theSound);
  460.             }
  461. }